<?php

namespace App\Http\Controllers;

use App\Models\SmsRecord;
use App\Services\SmsService;
use App\Transformers\SmsRecordTransformer;
use Illuminate\Http\Request;
use App\Jobs\SmsSend;
use Illuminate\Support\Facades\Auth;

class SmsRecordController extends SmsBaseController
{
    /**
     * @api {get} /api/smsRecord?include=fsUser,user 全部短信记录
     * @apiGroup Sms
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *
     *      {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "fs_user_id": 51,
     *                   "carry_user_id": 1,
     *                   "status": 1,
     *                   "value": "今天星期几",
     *                   "created_at": "2019-01-21 14:28:24",
     *                   "updated_at": "2019-01-21 14:28:28"
     *               }
     *           ],
     *               "meta": {
     *                   "pagination": {
     *                       "total": 1,
     *                       "count": 1,
     *                       "per_page": 10,
     *                       "current_page": 1,
     *                       "total_pages": 1,
     *                       "links": null
     *                   }
     *               }
     *         }
     */
    public function index(Request $request, SmsRecord $smsRecord)
    {
        $builder = SmsRecord::query();
        if(!Auth::user()->hasPermissionTo('api.smsRecord.index.all')){
            $builder->where('carry_user_id',Auth::user()->id);
        }
        $res = $builder->orderByDesc('created_at')->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        return $this->response->paginator($res, new SmsRecordTransformer());
    }
    /**
     * @api {get} /api/curr/smsRecord?include=user,fsUser 当前登陆短信记录
     * @apiGroup Sms
     *
     * @apiHeader {String} Authorization Bearer+空格+access_token [必填]
     * @apiHeader {String} Accept application/vnd.operationsystem.v1+json [必填]
     * @apiHeader {String} Content-Type application/json [必填]
     *
     *
     * @apiVersion 1.0.0
     * @apiSuccessExample {json} Success-Response:
     *       HTTP/1.1 200 OK
     *
     *      {
     *           "data": [
     *               {
     *                   "id": 1,
     *                   "fs_user_id": 51,
     *                   "carry_user_id": 1,
     *                   "status": 1,
     *                   "value": "今天星期几",
     *                   "created_at": "2019-01-21 14:28:24",
     *                   "updated_at": "2019-01-21 14:28:28"
     *               }
     *           ],
     *               "meta": {
     *                   "pagination": {
     *                       "total": 1,
     *                       "count": 1,
     *                       "per_page": 10,
     *                       "current_page": 1,
     *                       "total_pages": 1,
     *                       "links": null
     *                   }
     *               }
     *         }
     */
    public function currIndex(Request $request, SmsRecord $smsRecord){
        $userId = Auth::user()->id;
        $res = $smsRecord::where('carry_user_id',$userId)->paginate($request->limit ?? 10, ['*'], $pageName = 'page', $request->page ?? 1);
        return $this->response->paginator($res, new SmsRecordTransformer());
    }
}
